from selenium.webdriver.common.by import By
from base.mis.base import BasePage, BaseHandle
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class HomePage(BasePage):
    def __init__(self):
        super().__init__()
        self.avatar = (By.CSS_SELECTOR, ".avatar-wrapper")
        self.quit_info = (By.XPATH, "//li[contains(@class, 'el-dropdown-menu__item--divided')]//span[text()='退出']")
        self.content_manage = By.XPATH, "//div[contains(@class, 'el-submenu__title')]//span[text()='内容管理']"
        # self.content_manage = By.XPATH, "//*[@id='app']/div/div[1]/ul/div[2]/li/div"
        self.content_audit = By.XPATH, "//*[@class='current3']/li[3]//a"
        # 通知框关闭按钮
        self.notification_close = (By.CSS_SELECTOR, ".el-notification__closeBtn")

    def close_notification(self):
        """关闭通知框"""
        try:
            close_btn = WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable(self.notification_close))
            close_btn.click()
            print("通知框已关闭")
        except:
            print("没有找到通知框或通知框已关闭")

    def find_avatar(self):
        """查找用户头像"""
        # 先尝试关闭通知框
        self.close_notification()
        # 等待并返回可点击的用户头像
        return WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.avatar))

    def find_quit_info(self):
        """查找退出按钮"""
        # 先点击用户头像
        self.find_avatar().click()
        # 等待退出按钮可见并点击
        return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.quit_info))

    def get_quit_info(self):
        """获取退出信息"""
        # 点击退出按钮并获取文本
        quit_btn = self.find_quit_info()
        quit_text = quit_btn.text
        print("退出按钮文本:", quit_text)
        return quit_text

    # 如果后续完全不需要该方法，可以直接删除：
    # def find_content_manage(self):
    #     pass

    def find_content_audit(self):
        return self.get_element(self.content_audit)


class HomeHandle(BaseHandle):
    def __init__(self):
        self.home_page = HomePage()

    def get_quit_info(self):
        """获取退出按钮文本"""
        return self.home_page.get_quit_info()


class HomeProxy:
    def __init__(self):
        self.home_handle = HomeHandle()

    def get_quit(self):
        return self.home_handle.get_quit_info()
